﻿using System;
using System.Windows;
using System.Windows.Browser;
using Microsoft.Silverlight.Testing;
using Ninject.Core;
using Odin.Contracts.Data;
using Odin.Silverlight.Modules;

namespace Odin.Silverlight
{
	public partial class App : Application
	{
		public static IKernel Kernel;

		public App()
		{
			this.Startup += this.Application_Startup;
			this.Exit += this.Application_Exit;
			this.UnhandledException += this.Application_UnhandledException;

			Kernel = new StandardKernel(new ServiceModule());

			InitializeComponent();
		}

		private void Application_Startup(object sender, StartupEventArgs e)
		{
			var testRunner = Kernel.Get<TestRunner>();
			if (HtmlPage.Document.QueryString.ContainsKey(Session.SessionIdentifierKey))
			{
				var identifier = HtmlPage.Document.QueryString[Session.SessionIdentifierKey];
				testRunner.Start(identifier,
					s => Deployment.Current.Dispatcher.BeginInvoke(
					     	() => this.RootVisual = UnitTestSystem.CreateTestPage(s)));
			}
		}

		private void Application_Exit(object sender, EventArgs e)
		{

		}
		private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
		{
			if (!System.Diagnostics.Debugger.IsAttached)
			{
			}
		}
	}
}
